<template>
  <div>
    <div class="info">
      <div class="title">
        <span>修改密码</span>
      </div>
      <hr/>
      <div class="personal">
        <el-form :validate-on-rule-change="false" :model="registerForm" ref="registerForm" label-width="100px" class="demo-ruleForm" :rules="rules">
          <el-form-item prop="oldPassword" label="原始密码" >
            <el-input v-model="registerForm.oldPassword" placeholder="原始密码"></el-input>
          </el-form-item>
          <el-form-item prop="newPassword" label="新密码">
            <el-input v-model="registerForm.newPassword" placeholder="新密码"></el-input>
          </el-form-item>
          <el-form-item prop="surePassword" label="确认密码">
            <el-input v-model="registerForm.surePassword" placeholder="确认密码"></el-input>
          </el-form-item>
        </el-form>
        <div class="btn">
          <div @click="saveMsg">保存</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
name: "UpdatePassword",
  data() {
    return {
      registerForm: {
        oldPassword:'',
        newPassword:'',
        surePassword:''
      },
      rules: {
        oldPassword: [
          { required: true, trigger: 'blur', message: '请输入原始密码' }
        ],
        newPassword: [
          { required: true, trigger: 'blur', message: '请输入新密码' }
        ],
        surePassword: [
          { required: true, trigger: 'blur', message: '请输入确认密码' }
        ]
      }
    }
  },
  methods:{
   async saveMsg(){
     let result=await this.$API.reqConsumerLogin(this.userName,this.registerForm.oldPassword)
     if (result.code==10000){
       if (this.registerForm.newPassword==this.registerForm.surePassword){
           let result=await this.$API.reqUpdateConsumerPassword(this.userId,this.registerForm.newPassword)
           if (result.code==10000){
             this.$notify.success('修改密码成功')
             localStorage.removeItem('Token')
             this.$store.commit('LOGININFO',true)
             await this.$router.push({path:'/login',query:{name:'登录'}})
           }else {
             this.$notify.error('系统繁忙')
           }
       }
     }
    }
  },
computed: {
...mapState({
    userName: state => state.home.userName,
    userId: state => state.home.userId
  })
}
}
</script>

<style lang="scss" scoped>
@import '../assets/css/info.scss';
</style>
